var head;

function go() {
  head = document.getElementsByTagName('head')[0];
  fetchJSON('http://www.google.com/reader/public/javascript/user/02604812956666611325/state/com.google/starred?n=5&callback=jsonReader');
  fetchJSON('http://www.twitter.com/t/status/user_timeline/954321?callback=jsonTwitter&count=5');
  fetchJSON('http://api.flickr.com/services/feeds/photos_public.gne?id=7603495@N07&format=json');
}

function fetchJSON(src) {
  var script = document.createElement('script');
  script.src = src;
  head.appendChild(script);
}

function jsonFlickrFeed(obj) {
  var div = document.getElementById('photostream');
  var header = createElement('div');
  header.className = 'header';
  header.innerHTML = "Photostream";
  div.appendChild(header);
  
  var holder = createElement('div');
  holder.className="content";
  div.appendChild(holder);
  items = obj.items;
  for (var i = 0; i < 9; i++) {
    var item = items[i];
    var img = createElement('img');
    var a = createElement('a');
    img.border = 0;
    img.width = 75;
    img.height = 75;
    img.title = item.title;
    img.alt = "Flickr Photo";
    a.href =  item.link;
    img.src = item.media.m.replace('_m.', '_s.');
    holder.appendChild(a);
    a.appendChild(img);
    holder.appendChild(document.createTextNode(" "));
  }
}

function jsonTwitter(obj) {
  var div = document.getElementById('twitter');
  var header = createElement('div');
  header.className = 'header';
  header.innerHTML = "Twitter Updates";
  div.appendChild(header);

  var holder = createElement('div');
  holder.className="content";
  div.appendChild(holder);

  var ul = createElement('ul');
  holder.appendChild(ul);

  for (var i = 0; i < obj.length; i++) {
    var item = obj[i];
    var li = createElement('li');

    li.innerHTML = item.text;
    ul.appendChild(li);


    var time = createElement('div');
    time.innerHTML = getPrettyTime(item.created_at);
    time.style.textAlign = "right";
    time.style.fontStyle = "italic";
    li.appendChild(time);
  }
  var a = createElement('a');
  a.href = 'http://twitter.com/nanaze/';
  a.innerHTML = "twitter.com/nanaze";
  holder.appendChild(a);
}

var units = {second: 1, minute: 60, hour: 3600,
             day: 86400, week: 604800, month: 2592000,
             year: 31536000}

function getPrettyTime(timeString) {
  timeString - fixDate(timeString);
  var secs = new Date().getTime() -
             new Date(timeString).getTime();
  secs = Math.floor(secs / 1000);

  if (secs <= 0) return "just added"
  var lastUnit = 'second';
  for (unit in units) {
    if (units[unit] > secs) break;
    lastUnit = unit;    
  }

  var floored = Math.floor(secs / units[lastUnit]);

  if (floored != 1) lastUnit = lastUnit + "s";
  
  return [floored, lastUnit, 'ago'].join(" ");
}

// borrowed from Mr. President
function fixDate(d) {
    var a = d.split(' ');
    var year = a.pop();
    return a.slice(0, 3).concat([year]).concat(a.slice(3)).join(' ');
}

function jsonReader(obj) {
  var div = document.getElementById('googlereader');
  var header = createElement('div');
  header.className = 'header';
  header.innerHTML = 
    "Shared in <a href=\"http://reader.google.com/\">Reader</a>";
  div.appendChild(header);
  
  var holder = createElement('div');
  holder.className="content";
  div.appendChild(holder);

  var ul = createElement('ul');
  holder.appendChild(ul);

  var lis = [];

  var items = obj.items;
  for (var i = 0; i < items.length; i++) {
    var item = items[i];
    var li = createElement('li');
    li.innerHTML = ["<a href=\"", item.alternate.href,
"\">", item.title, "</a> - <a href=\"",
item.origin.htmlUrl, "\">", item.origin.title,
"</a>"].join("");
    ul.appendChild(li);
  }

  var a = createElement('a');
  a.href = 'http://www.google.com/reader/public/atom/user/02604812956666611325/state/com.google/broadcast';
  a.innerHTML = 'Shared Items Feed';
  holder.appendChild(a);
}

function createElement(tag) {
  return document.createElement(tag);
}